//网页加载完毕后首先执行的代码
$().ready(function () {
    //栏目的下标
    //".book-eBook-netBook .category li"
    //".book-eBook-netBook .content-left .content"
    switchCategory(".book-eBook-netBook .category li", ".book-eBook-netBook .content-left .content");
    switchCategory(".clothing .category li", ".clothing .content");
    switchBook();
    time = setInterval("slideImg();", 3000);
    chooseImg();
    moveToLeft();
})


//=====================================首行
/**
 *
 * @param {*} address 设置地址
 */
function setAddress(address) {
    // document.getElementById("current-address").innerText = address;
    document.getElementById("current-address").innerText = address.innerText;
    // document.getElementById("address-list").style.display="none";
}

/**
 * 展开列表
 * 1.获取img元素
 * 2.设置img元素src属性
 * 方法二:
 * 1.获取li元素
 * 2.设置li的宽度
 * 3.设置li的背景图片位置
 */

function changeIcoToUp(v) {
    document.getElementById(v).style.backgroundPositionY = "-322px";
}

/**
 * 收起列表
 */
function changeIcoToDown(v) {
    document.getElementById(v).style.backgroundPositionY = "-306px";
}

//=========================================第二行
/**
 * 得到焦点清除内容
 */
function clearSearchFrameContent() {
    var v = document.getElementById("search-condition");
    if (v.value == "搜索一下,会有惊喜~") {
        v.value = "";
    }
}

/**
 * 失去焦点保持搜索框内容
 */
function keepSearchFrameStatus() {
    var v = document.getElementById("search-condition");
    if (v.value == "") {
        v.value = "搜索一下,会有惊喜~";
    }
    $("#show-good-result").hide();
}

/**
 * 设置选中的搜索类别
 */
function changeCategory(category) {
    document.getElementById("current-category-text").innerText = category;
    // document.getElementById("type-list").style.display="none";
}

function searchGoods(obj) {
    var goodname = obj.value;
    $.ajax({
        "url": "/searchPromptServlet",
        "data": {"goodname": goodname},
        "type": "get",
        "dataType": "json",
        "success": function (data) {
            console.log(data)
            $("#show-good-result").html("");
            //获取json数据
            var len = data.length;
            for (var i = 0; i < len; i++) {
                $("#show-good-result").append("<div>" + data[i].name + "</div>")
            }
            $("#show-good-result").show();
            console.log(data.name);
        },
        "error": "请求失败"
    });
}


//===================================第三行
/**
 * 定时播放图片
 */
var now = 1;

function nextBigPicture() {
    for (var i = 1; i <= 8; i++) {
        document.getElementById("choose-" + i).style.backgroundColor = "";
    }
    var str = document.getElementById("big-ad-img").src;//1.获取图片得绝对路径
    str = str.replace("http://127.0.0.1:8848/dangdang/", "");//2.设置图片为相对路径
    //3.替换图片为下一张图片
    if (now == 8) {
        document.getElementById("big-ad-img").src = str.replace("content_ad_8", "content_ad_1");
        document.getElementById("choose-" + 1).style.backgroundColor = "red";
        now = 1;
    } else {
        document.getElementById("choose-" + (now + 1)).style.backgroundColor = "red";
        document.getElementById("big-ad-img").src = str.replace("content_ad_" + now, "content_ad_" + (now + 1));
        now++;
    }
}

var t;

function showBigAd() {
    t = setInterval("nextBigPicture()", 2000);
}

/**
 * @param {Object} setNum
 * 选择图片
 */
function choosePic(setNum) {
    if (setNum == 0) {
        setNum = 8;
    } else if (setNum == 9) {
        setNum = 1;
    }
    clearInterval(t);
    for (var i = 1; i <= 8; i++) {
        document.getElementById("choose-" + i).style.backgroundColor = "";
    }
    document.getElementById("choose-" + setNum).style.backgroundColor = "red";
    var str = document.getElementById("big-ad-img").src;//1.获取图片得绝对路径
    str = str.replace("http://127.0.0.1:8848/dangdang/", "");//2.设置图片为相对路径
    //3.替换图片为下一张图片
    document.getElementById("big-ad-img").src = str.replace("content_ad_" + now, "content_ad_" + setNum);
    now = setNum;
}

function recover() {
    showBigAd();
}

/**
 * 让按钮显示
 */
function showBtn() {
    document.getElementsByClassName("choose-left")[0].style.display = "block";
    document.getElementsByClassName("choose-right")[0].style.display = "block";
}

/**
 * 让按钮隐藏
 */
function hideBtn() {
    document.getElementsByClassName("choose-left")[0].style.display = "none";
    document.getElementsByClassName("choose-right")[0].style.display = "none";
}

/**
 * 点击按键左移一个图片
 */
function moveToLeftPicture() {
    choosePic(now - 1);
    recover();
}

/**
 * 点击按键右移一个图片
 */
function moveToRightPicture() {
    choosePic(now + 1);
    recover();
}

/**
 * 切换信息公告和服务公告
 * @param {*} num
 */
function showDetails(num) {
    // var info_ul= document.getElementById("announce-1").style.display;
    // var service_ul= document.getElementById("announce-2").style.display;
    // var info= document.getElementById("info").style.backgroundColor;
    // var service= document.getElementById("service").style.backgroundColor;
    // console.log("info_ul:"+info_ul+"\t"+"info:"+info+"\r"+"service_ul:"+service_ul+"service:"+service);
    if (num == 1) {
        document.getElementById("announce-1").style.display = "block";
        document.getElementById("info").style.backgroundColor = "#fff";

        document.getElementById("announce-2").style.display = "none";
        document.getElementById("service").style.backgroundColor = "#e6e6e6";
    }
    if (num == 2) {
        document.getElementById("announce-1").style.display = "none";
        document.getElementById("info").style.backgroundColor = "#e6e6e6";

        document.getElementById("announce-2").style.display = "block";
        document.getElementById("service").style.backgroundColor = "#fff";
    }
}


var j = 2;

function nextSmallPicture() {
    var str = document.getElementById("right-down-img").src;//1.获取图片得绝对路径 http://127.0.0.1:8848/dangdang/img/right-ad/right-ad-down/right_ad_down_1.jpg
    str = str.replace("http://127.0.0.1:8848/dangdang/", "");//2.设置图片为相对路径
    //3.替换图片为下一张图片
    if (j == 4) {
        document.getElementById("right-down-img").src = str.replace("right_ad_down_3", "right_ad_down_1");
        j = 2;
    } else {
        document.getElementById("right-down-img").src = str.replace("right_ad_down_" + (j - 1), "right_ad_down_" + j);
        j++;
    }
}

// right-down-img
setInterval(function () {
    nextSmallPicture();
}, 4000);

/**************************************************************************************************************** */

function moveToLeft() {
    $(".seckill .seckill-right .seckill-right-goods img").hover(function () {
        //鼠标移入
        $(this).animate({left: '-5px'}, 200);
    }, function () {
        //鼠标移出
        $(this).animate({left: '0px'}, 200);
    });
}

/**
 * 图书.电子书.网络文学 切换栏目
 * @param {需要切换的栏目} categoryStr
 * @param {*需要切换的内容} contentStr
 */
function switchCategory(categoryStr, contentStr) {
    var book_index;
    $(categoryStr).mouseover(function () {
        //鼠标移入
        //1.通过隐藏标签的方法,先关闭上次显示的栏目,将当前鼠标指向的元素css切换为无特征
        book_index = $(this).index();
        // alert(index);
        $(categoryStr).each(function (i) {
            if (i != book_index) {
                $(this).removeClass("after-hover");
                $(contentStr).eq(i).addClass("hide-class");
            } else {
                //2.为标签添加特征,同时将相应的栏目显示,并将之前显示的隐藏
                $(categoryStr).eq(book_index).addClass("after-hover");
                $(contentStr).eq(book_index).removeClass("hide-class");
            }
        });
    });
}

function switchBook() {
    var book_index;
    $(".book-eBook-netBook .content-right li").mouseover(function () {
        book_index = $(this).index();
        $(".book-eBook-netBook .content-right li").each(function (i) {
            if (i != book_index) {
                //移除鼠标不在的地方的特征
                $(this).css("height", "42px");
                $(this).children().eq(0).removeClass("hide-class");
                $(this).children().eq(1).addClass("hide-class");
            } else {
                //为鼠标所在的地方添加特征
                $(this).css("height", "100px")
                $(this).children().eq(0).addClass("hide-class");
                $(this).children().eq(1).removeClass("hide-class");
            }
        });
    });
}

var time;
var imgIndex = 3;//下一张要显示的图片索引,取值为0,1,2,3,4
var imgPoint = 0;//取值为1,2,3,4,5
var cloth_recommend_1 = "#cloth-recommend-1";
var cloth_recommend_2 = "#cloth-recommend-2";

function slideImg() {
    //图一向左移动384px,移动结束后,将图片切换为图3,将left偏移设置为384px
    $(cloth_recommend_1).animate({left: '-384px'}, 500, function () {
        $(cloth_recommend_1).attr("src", "img/clothing/recommend/recommend-" + imgIndex + ".jpg");
        $(cloth_recommend_1).css("left", "384px");
    });
    //图二向左移动384px
    $(cloth_recommend_2).animate({left: '0px'}, 500, function () {
        $(".clothing .right-side li:eq(" + imgPoint + ")").css("background-color", " rgba(100, 100, 100, 0.5)");
        imgPoint++;
        if (imgPoint == 5) {
            imgPoint = 0;
        }
        //切换图片指下标
        $(".clothing .right-side li:eq(" + imgPoint + ")").css("background-color", "white");
        imgIndex++;
        if (imgIndex == 6) {
            imgIndex = 1;
        }
        //交换两个标签执行的动画
        temp = cloth_recommend_2;
        cloth_recommend_2 = cloth_recommend_1;
        cloth_recommend_1 = temp;
    });
}

function chooseImg() {
    $(".clothing .right-side li").mouseover(function () {
        clearTimeout(time);
        imgIndex = $(this).index();
        //imgIndex1,第一张图片索引
        //imgIndex2,第二张图片索引
        var imgIndex1, imgIndex2;
        imgNumber1 = imgIndex + 1;
        imgNumber2 = imgIndex + 2;
        if (imgNumber1 == 5) {
            imgNumber2 = 1;
        }
        $(cloth_recommend_1).attr("src", "img/clothing/recommend/recommend-" + imgNumber1 + ".jpg");
        $(cloth_recommend_2).attr("src", "img/clothing/recommend/recommend-" + imgNumber2 + ".jpg");
        $(".clothing .right-side li").each(function () {
            $(this).css("background-color", " rgba(100, 100, 100, 0.5)");
        });
        //切换图片下标
        $(".clothing .right-side li:eq(" + imgIndex + ")").css("background-color", "white");
        imgPoint = imgIndex;
        //确定下一张要显示的图片索引
        //如果正在显示的第二张图片索引小于4,则将下一张要显示的图片索引设置为在当前显示的第二张图片索引+1
        //如果正在显示的第二张图片索引==5,则将下一张要显示的图片索引设置为1
        imgIndex = (imgNumber2 == 5) ? (1) : (imgNumber2 + 1);
        time = setInterval("slideImg();", 3000);
    });
}